﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Manage
{
    public partial class SystemMenu : PageBaseBackend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindPMenu();
                bindMenu();
                if (Request["menuID"] != null)
                {
                    initMenu();
                }
            }
        }

        //初始化信息
        private void initMenu() 
        {
            Model.Menu menu = BLL.MenuBLL.Instance.GetDetail(Request["menuID"].ToString());
            if (menu != null)
            {
                txtMenuName.Text = menu.MenuName;
                txtPageUrl.Text = menu.PageUrl;
                ddlpMenu.SelectedValue = menu.ParentID;
                btnSubmit.Text = "更新"; 
            }
        }

        //绑定父菜单
        private void bindPMenu()
        {
            ddlpMenu.DataSource = BLL.MenuBLL.Instance.GetMenuByParentID("0");
            ddlpMenu.DataTextField = "MenuName";
            ddlpMenu.DataValueField = "MenuID";
            ddlpMenu.DataBind();
            
            ddlpMenu.Items.Insert(0,new ListItem("==添加父菜单==","0"));
        }

        private void bindMenu()
        {
            repMenu.DataSource = BLL.MenuBLL.Instance.GetList();
            repMenu.DataBind();
        }

        //添加菜单
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Model.Menu menu = new Model.Menu();
            if (Request["menuID"] != null)
            {
                menu = BLL.MenuBLL.Instance.GetDetail(Request["menuID"].ToString());
            }
            if (txtMenuName.Text.Trim() == "")
            {
                lbMessage.Text = "菜单名称不能为空！";
                return;
            }
            else
            {
                menu.MenuName = txtMenuName.Text.Trim();

            }
            menu.IconPath = "";
            menu.PageUrl = txtPageUrl.Text.Trim();
            if (ddlpMenu.SelectedIndex!=0&&txtPageUrl.Text.Trim()=="")
            {
                lbMessage.Text = "页面路径不能为空";
                return;
            }
            menu.ParentID = ddlpMenu.SelectedValue;
            if (Request["menuID"] == null)
            {
                if (BLL.MenuBLL.Instance.GetMenuByName(menu.MenuName) != null)
                {
                    lbMessage.Text = "该名称已存在！";
                }
                else
                {
                    menu.MenuID = Guid.NewGuid().ToString();
                    menu.State = (int)Common.Enum.auvState.Enabled;
                    menu.IsDel = (int)Common.Enum.auvDel.NotDel;
                    menu.IsBack = true;
                    menu.CreateDate = DateTime.Now;
                    menu.UpdateDate = DateTime.Now; ;
                    menu.BeginDate = DateTime.Now;
                    menu.EndDate = DateTime.Now; ;
                    BLL.MenuBLL.Instance.Add(menu);
                    lbMessage.Text = "添加成功！";
                    bindMenu();
                    bindPMenu();
                }
            }
            else
            {
                BLL.MenuBLL.Instance.Update(menu);
                lbMessage.Text = "更新成功";
                bindMenu();
                bindPMenu();
            }
        }
    }
}